<input type="file" id="file-input" accept=".json, .txt" />
<button id="save-button">Save to IndexedDB</button><br>
<input type="text" id="retrieve-filename" placeholder="Enter filename to retrieve" />
<button id="retrieve-button">Retrieve from IndexedDB</button><br>

<script>
document.getElementById('retrieve-button').addEventListener('click', () => {
    const request = indexedDB.open('myDatabase', 1);

    request.onupgradeneeded = (event) => {
        const db = event.target.result;
        if (!db.objectStoreNames.contains('files')) {
            const store = db.createObjectStore('files', { keyPath: 'name' });
            store.put({ name: 'example.txt', content: 'This is an example file content' });
        }
        console.log('Default data added to IndexedDB');
    };

    request.onsuccess = (event) => {
        const db = event.target.result;
        const transaction = db.transaction('files', 'readonly');
        const store = transaction.objectStore('files');
        const getRequest = store.getAll();

        getRequest.onsuccess = (event) => {
            const files = event.target.result;
            if (files.length > 0) {
                files.forEach(file => {
                    const blob = new Blob([file.content], { type: 'text/plain' });
                    const link = document.createElement('a');
                    link.href = URL.createObjectURL(blob);
                    link.download = file.name;
                    link.click();
                });
                console.log('Files retrieved and downloaded');
            } else {
                console.error('No files found in IndexedDB');
            }
        };

        getRequest.onerror = (event) => {
            console.error('Retrieve error:', event.target.error);
        };
    };

    request.onerror = (event) => {
        console.error('IndexedDB error:', event.target.error);
    };
});

</script>
<script>

document.getElementById('save-button').addEventListener('click', () => {
    const fileInput = document.getElementById('file-input');
    const file = fileInput.files[0];
    if (file) {
        const reader = new FileReader();
        reader.onload = (event) => {
            const content = event.target.result;
            const request = indexedDB.open('myDatabase', 1);

            request.onupgradeneeded = (event) => {
                const db = event.target.result;
                db.createObjectStore('files', { keyPath: 'name' });
            };

            request.onsuccess = (event) => {
                const db = event.target.result;
                const transaction = db.transaction('files', 'readwrite');
                const store = transaction.objectStore('files');
                store.put({ name: file.name, content: content });
                transaction.oncomplete = () => {
                    console.log('File saved to IndexedDB');
                };
                transaction.onerror = (event) => {
                    console.error('Transaction error:', event.target.error);
                };
            };

            request.onerror = (event) => {
                console.error('IndexedDB error:', event.target.error);
            };
        };
        reader.readAsText(file);
    } else {
        console.error('No file selected');
    }
});
</script></script>